package notification

import (
	"log"

	"github.com/fisker/zjump-backend/internal/repository"
	"gorm.io/gorm"
)

// InitFromDatabase 从数据库初始化通知管理器
func InitFromDatabase(db *gorm.DB) *NotificationManager {
	nm := NewNotificationManager()
	nm.db = db      // 保存数据库连接以便后续重新加载
	nm.loadConfig() // 加载配置
	return nm
}

// loadConfig 从数据库加载通知配置（内部方法）
func (nm *NotificationManager) loadConfig() {
	if nm.db == nil {
		log.Printf("[Notification] Cannot load: database connection is nil")
		return
	}

	// 从数据库加载通知配置
	settingRepo := repository.NewSettingRepository(nm.db.(*gorm.DB))
	settings, err := settingRepo.GetByCategory("notification")
	if err != nil {
		log.Printf("[Notification] Failed to load notification settings: %v", err)
		return
	}

	settingsMap := make(map[string]string)
	for _, s := range settings {
		settingsMap[s.Key] = s.Value
	}

	log.Printf("[Notification] Loading notification settings from database...")

	// 获取写锁，准备更新配置
	nm.mu.Lock()
	defer nm.mu.Unlock()

	// 清空现有通知器
	nm.notifiers = make([]Notifier, 0)
	nm.enabled = false

	var notifiersAdded int

	// 配置飞书通知
	if settingsMap["enableFeishu"] == "true" {
		webhookURL := settingsMap["feishuWebhook"]
		secret := settingsMap["feishuSecret"]
		if webhookURL != "" {
			feishu := NewFeishuNotifier(webhookURL, secret)
			nm.AddNotifier(feishu)
			notifiersAdded++
			truncatedURL := webhookURL
			if len(webhookURL) > 50 {
				truncatedURL = webhookURL[:50] + "..."
			}
			log.Printf("[Notification]  Feishu notifier enabled (webhook: %s)", truncatedURL)
		} else {
			log.Printf("[Notification]  Feishu enabled but webhook URL is empty")
		}
	}

	// 配置钉钉通知
	if settingsMap["enableDingTalk"] == "true" {
		webhookURL := settingsMap["dingTalkWebhook"]
		secret := settingsMap["dingTalkSecret"]
		if webhookURL != "" {
			dingtalk := NewDingTalkNotifier(webhookURL, secret)
			nm.AddNotifier(dingtalk)
			notifiersAdded++
			truncatedURL := webhookURL
			if len(webhookURL) > 50 {
				truncatedURL = webhookURL[:50] + "..."
			}
			log.Printf("[Notification]  DingTalk notifier enabled (webhook: %s)", truncatedURL)
		} else {
			log.Printf("[Notification]  DingTalk enabled but webhook URL is empty")
		}
	}

	// 配置企业微信通知
	if settingsMap["enableWeChat"] == "true" {
		webhookURL := settingsMap["wechatWebhookURL"]
		if webhookURL != "" {
			wechat := NewWeChatNotifier(webhookURL)
			nm.AddNotifier(wechat)
			notifiersAdded++
			truncatedURL := webhookURL
			if len(webhookURL) > 50 {
				truncatedURL = webhookURL[:50] + "..."
			}
			log.Printf("[Notification]  WeChat notifier enabled (webhook: %s)", truncatedURL)
		} else {
			log.Printf("[Notification]  WeChat enabled but webhook URL is empty")
		}
	}

	// 如果有任何通知器被添加，启用通知管理器
	if notifiersAdded > 0 {
		nm.enabled = true
		log.Printf("[Notification]  Notification system enabled with %d notifier(s)", notifiersAdded)
	} else {
		nm.enabled = false
		log.Printf("[Notification]  No notifiers configured, notification system disabled")
	}
}

// ReloadFromDatabase 重新从数据库加载通知配置（公开方法，用于动态更新配置）
func (nm *NotificationManager) ReloadFromDatabase() {
	log.Printf("[Notification] Reloading notification configuration from database...")
	nm.loadConfig()
}
